/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import com.mycompany.psc.data.TenderType;
import com.mycompany.psc.ejb.ReportableItemsManager;
import com.mycompany.psc.entities.TenderItems;
import com.mycompany.psc.entities.TenderItemsData;
import com.mycompany.psc.entities.Users;
import com.mycompany.psc.producers.LoggedIn;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author A7med
 */
@Named
@SessionScoped
public class ReportsBean implements Serializable {

   
    private TenderType t;
    @EJB
    ReportableItemsManager rm;
    @Inject
    ProjectsBean p;
    private List<TenderItems> filteredResource;
    private String title;
    private String code;
    @Inject
    @LoggedIn
    Users current;
    private TenderItems selectedResource;
    private TenderItemsData newItem=new TenderItemsData();
    private TenderItemsData editableItem;
    private float expences;
    private int amount;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public float getExpences() {
        return expences;
    }

    public void setExpences(float expences) {
        this.expences = expences;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<TenderItems> getFilteredResource() {
        return filteredResource;
    }

    public void setFilteredResource(List<TenderItems> filteredResource) {
        this.filteredResource = filteredResource;
    }

   

    /**
     * @return the t
     */
    public TenderType getT() {
        return t;
    }

    /**
     * @param t the t to set
     */
    public void setT(TenderType t) {
        this.t = t;
    }

    public TenderType[] allTypes() {
        return TenderType.values();
    }

    public List<TenderItems> getResources(TenderType t) {
        return rm.getResources(t, p.getSelectedReportProject());
    }
   public List<TenderItemsData> getChildResources(TenderType t) {
        List<TenderItems> list = rm.getResources(t, p.getSelectedReportProject());
        List<TenderItemsData> out=new ArrayList<>();
        for(TenderItems ti:list){
            out.addAll(ti.getTenderItemsDataList());
        }
        return out;
    }
 public void updateItem(){
        rm.update(selectedResource);
    }
    public void updateChildItem(){
        rm.update(editableItem);
    }
    public void save(TenderType t) {
        System.err.println("current" + current.getName());
        rm.save(t, p.getSelectedReportProject(), current, title,expences,amount,code);
    }
   
    /**
     * @return the selectedResource
     */
    public TenderItems getSelectedResource() {
        return selectedResource;
    }
public void setSelected(TenderItems res){
    System.out.println("selected:"+res);
}
    /**
     * @param selectedResource the selectedResource to set
     */
    public void setSelectedResource(TenderItems selectedResource) {
        this.selectedResource = selectedResource;
    }
    public void deleteResource(){
        rm.delete(selectedResource);
    }
    
    public void saveData(){
        System.err.println("notes;"+newItem.getNotes());
        if(newItem.getNotes().equals("")){
            newItem.setNotes("No Notes Entered");
        }
        newItem.setParent(selectedResource);
        selectedResource.getTenderItemsDataList().add(newItem);
        rm.update(selectedResource);
    }

    /**
     * @return the newItem
     */
    public TenderItemsData getNewItem() {
        return newItem;
    }

    /**
     * @param newItem the newItem to set
     */
    public void setNewItem(TenderItemsData newItem) {
        this.newItem = newItem;
    }
    public void setResource(TenderItems t){
        System.out.println("tt="+t.getTitle());
    }

    public TenderItemsData getEditableItem() {
        return editableItem;
    }

    public void setEditableItem(TenderItemsData editableItem) {
        this.editableItem = editableItem;
    }
}
